package tr.edu.yildiz.ce.app_manager.ui.components.form;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.sql.Blob;

import org.hibernate.Hibernate;

import tr.edu.yildiz.ce.app_manager.model.Application;
import tr.edu.yildiz.ce.app_manager.ui.AppManagerApplication;
import tr.edu.yildiz.ce.app_manager.ui.components.DapUpload;

import com.vaadin.ui.CheckBox;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Upload;

public class ApplicationForm extends BasicForm<Application> implements Upload.Receiver{

	TextField applicationName = null;
	TextField applicationVersion = null;
	CheckBox autoDeploy = null;
	DapUpload upload;
	ByteArrayOutputStream bout ;
		
	public ApplicationForm(Application applicationn,AppManagerApplication app) {
		super(app);
		model = applicationn;
		applicationName = new TextField("Application Name");;
		applicationVersion = new TextField("Application Version");
		autoDeploy = new CheckBox("Auto Deploy");
		if (model.getApplicationId() != null){
			if (model.isAutoDeploy() != null)
				autoDeploy.setValue(new Boolean(model.isAutoDeploy()));
			applicationName.setValue(model.getApplicationName());
			applicationVersion.setValue(model.getApplicationVersion());
		}
		addField("Application Name", applicationName);
		addField("Application Version", applicationVersion);
		addField("", autoDeploy);
		upload = new DapUpload("Application Definition File ",this);
		addField("Upload", upload);
		
		initButtons();
	}
	
	@SuppressWarnings("deprecation")
	@Override
	protected void preSave() {
		model.setApplicationName(applicationName.getValue().toString());
		model.setApplicationVersion(applicationVersion.getValue().toString());
		model.setAutoDeploy(autoDeploy.booleanValue());
		if (bout != null)
			model.setAppXml(Hibernate.createBlob(bout.toByteArray()));
	}

	@Override
	public OutputStream receiveUpload(String filename, String mimeType) {
		bout = new ByteArrayOutputStream();
		return bout;
	}
	
	
	
}
